package com.atguigu.gmall.product.config;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * @author dyqstart
 * @create 2021-08-03-13:16
 * @since 1.0.0
 */
//@Configuration
@Component
@ConfigurationProperties(prefix = "minio")
@Data
public class MinioConfig {

    private String url;
    private String accessKey;
    private String secretKey;
    private String defaultBucket;

    @Bean
    public MinioClient minioClient(){
        try {
            MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
            boolean exists = minioClient.bucketExists(defaultBucket);
            if (!exists){
                minioClient.makeBucket(defaultBucket);
            }
            return minioClient;
        } catch (Exception e) {
            return null;
        }
    }
}
